import axios from 'axios'
import type {AxiosInstance} from 'axios'
import { useCounterStore } from '@/stores/counter';

const instance: AxiosInstance = axios.create({
    baseURL: "/",
    timeout: 100000000,
})

instance.interceptors.request.use(function (config) {
    const use = useCounterStore()
    // config.headers.token = use.userInfo.token
    return config;
}, function (error) {

    return Promise.reject(error);
});

instance.interceptors.response.use(function (response) {
    return response;
}, function (error) {
    return Promise.reject(error);
});
interface IOptions {
    url:string
    method?:string
    data?:any
}
export function ajax(options:IOptions) {
    const { url, method = 'get', data = {} } = options
    if (method.toUpperCase() === 'GET') {
        return instance.get(url, {
            params: data
        })
    } else {
        return instance.post(url, data)
    }
}